﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using webapp4U;
using webapp4U.UI;
using webapp4U.BOL;
public partial class Products_Default : BasePage
{
    string InClassKey = "";
    string CommodKey = "";
    string error = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        InClassKey = Globals.GetStringFromQueryString("InClassKey");
        CommodKey = Globals.GetStringFromQueryString("CommodKey");     
        Load_Title();        
        Pager.IndexChanged += new EventHandler(PageIndex_Changed);
        
        if (!Page.IsPostBack)
        {
            try
            {
                Session["BackPageUrl"] = Request.Url.AbsoluteUri;
            }
            catch
            {

            }
            this.DataBind();
           
        }
    }

    private void PageIndex_Changed(Object sender, EventArgs e)
    {
        DataBind();
    }


    private void Load_Title()
    {
        Pager.PageSize = 3;
        if ( CommodKey !="")
        {
            
            // kiem tra xem can hien thi san pham hinh thuc nao
            List<webapp4U.BOL.InClassCategory> obj = webapp4U.BOL.InClassCategory.GetAllCategoryByCommodKey(CommodKey, Pager.PageIndex, Pager.PageSize, ref error);            
            // xem loai cha
            if (obj.Count > 0)
            {
                Title.TitleName = InClassCategory.GetNameCategoryByCommodKey(CommodKey, ref error);
                ProductByParent.Visible = true;                
                Panel_ProductByCate.Visible = false;
                Pager.Visible = false;
            }
            else // Commod chỉ có item
            {
                Title.TitleName = Title.TitleName = InClassCategory.GetNameCategoryByCommodKey(CommodKey, ref error);

                ProductByParent.Visible = false;                
                Panel_ProductByCate.Visible = true;
                Pager.Visible = true;
            }
        }
        else if (InClassKey != "")
        {
            Title.TitleName = InClassCategory.GetCommodNameCategoryByInClassKey(InClassKey, ref error) + " > " + InClassCategory.GetNameCategoryByInClassKey(InClassKey, ref error);

            ProductByParent.Visible = false;
            Panel_ProductByCate.Visible = true;
            Pager.Visible = true;
        }
        else
        {
            Title.TitleName = ResourceManager.GetString("Product_Category");
            ProductByParent.Visible = false;
            Panel_ProductByCate.Visible = false;
            Pager.Visible = false;
        }
    }


    protected string GetPrice(float UnitPrice)
    {
        if (UnitPrice > 0)
            return UnitPrice.ToString() + " VNĐ"; //Formatter.FormatMoney(UnitPrice)
        else
            return ResourceManager.GetString("Product_Price_Lienhe");
    }


    public override void DataBind()
    {
        // hien thi bao nhieu hinh tren 1 trang
        if (CommodKey != "")
        {
            List<webapp4U.BOL.InClassCategory> obj = webapp4U.BOL.InClassCategory.GetAllCategoryByCommodKey(CommodKey, Pager.PageIndex, Pager.PageSize, ref error);
            // xem loai cha
            if (obj.Count <=0)
            {
                Pager.PageSize = 12;
                ///comkey 
                List<Products> objl = Products.GetProductsByCommodKey(CommodKey, Pager.PageIndex, Pager.PageSize, ref error);
                ItemsDataList.DataSource = objl;
                ItemsDataList.DataBind();

                Pager.TotalRecords = Products.GetProductsCountByCommodKey(CommodKey, ref error);

                // kiem tra xem co can hien thi phan trang ko
                if (Pager.TotalRecords <= 1)
                    DisplayPager.Visible = false;
                else
                    DisplayPager.Visible = true;
            }
        }
        else
        {
            Pager.PageSize = 12;

            List<Products> obj = Products.GetProductsByInClassKey(InClassKey, Pager.PageIndex, Pager.PageSize, ref error);
            ItemsDataList.DataSource = obj;
            ItemsDataList.DataBind();

            Pager.TotalRecords = Products.GetProductsCountByInClassKey(InClassKey, ref error);

            // kiem tra xem co can hien thi phan trang ko
            if (Pager.TotalRecords <= 1)
                DisplayPager.Visible = false;
            else
                DisplayPager.Visible = true;
        }
    }

    //protected string GetButtonAddToCart(Double UnitPrice)
    //{
    //    if (UnitPrice > 0)
    //        //if (Globals.LangVietNam())
    //            return "<img src=" + Globals.ImagesUrl() + "AddToCart_Nho.gif border=0 />";
    //        //else
    //        //    return "<img src=" + Globals.ImagesUrl() + "AddToCart_Nho_en.gif border=0 />";
    //    return "";
    //}

    protected void ItemsDataList_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        webapp4U.UI.Button btnAddToCart = (webapp4U.UI.Button)e.Item.FindControl("btnAddToCart");
        webapp4U.UI.Button btnProductOrder = (webapp4U.UI.Button)e.Item.FindControl("btnProductOrder");

        //Image img = (Image)e.Item.FindControl("testImage");

        System.Web.UI.WebControls.Label lblPrice = (System.Web.UI.WebControls.Label)e.Item.FindControl("lblPrice");
        if (lblPrice.Text == webapp4U.ResourceManager.GetString("Product_Price_Lienhe"))
        {
            btnAddToCart.Visible = false;
            btnProductOrder.Visible = false;
            //img.Visible = false;
        }
        else
        {
            HtmlInputHidden txtItemkey = (HtmlInputHidden)e.Item.FindControl("txtItemkey");
            string CallBack = Page.ClientScript.GetCallbackEventReference(Page.Master.FindControl("ShoppingCart1"), "document.getElementById('" + txtItemkey.ClientID + "').value", "RateContent", "null");
            btnAddToCart.Attributes["onclick"] = String.Format("javascript:{0}", CallBack);

            string CallBack1 = Page.ClientScript.GetCallbackEventReference(Page.Master.FindControl("ShoppingCart1"), "'btnProductOrder' + document.getElementById('" + txtItemkey.ClientID + "').value", "RateContent", "null");
            btnProductOrder.Attributes["onclick"] = String.Format("javascript:{0}", CallBack1);
            //img.Attributes["onclick"] = String.Format("javascript:{0}", CallBack);
            //img.ImageUrl = Globals.ImagesUrl() + "AddToCart_nho.gif";
        }

    }

    protected void btn_ProductOrder_Click(object sender, EventArgs e)
    {
        //Response.Redirect(Globals.ApplicationPath + "Page/Products/shoppingcart.aspx?Action=DetailCart");
        //Products obj = Products.GetProductsByItemKey(ItemKey, ref error);
        //ShoppingCart shop = new ShoppingCart();
        //shop.AddedDate = DateTime.Now;
        //shop.ItemKey = obj.ItemKey;
        //shop.Description = obj.Description;
        //shop.UnitPrice = obj.UnitPrice;
        //shop.Quantity = 1;
        //shop.Subtotal = obj.UnitPrice;

        //List<ShoppingCart> ListShopCart = new List<ShoppingCart>();
        //if (Session["ShoppingCart"] == null)
        //{
        //    ListShopCart.Add(shop);
        //    Session["ShoppingCart"] = ListShopCart;
        //}
        //else
        //{
        //    ListShopCart = (List<ShoppingCart>)Session["ShoppingCart"];
        //    Session["ShoppingCart"] = ShoppingCart.AddItemToShoppingCart(ListShopCart, shop);
        //}

        //Response.Redirect(Globals.ApplicationPath + "Page/Products/shoppingcart.aspx?Action=DetailCart");
    }

}